package com.wanxi.servlet.product;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.model.ProductModel;
import com.wanxi.model.ResultModel;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/findAll")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的数据
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String startNormalPrice = req.getParameter("startNormalPrice");
        String endNormalPrice = req.getParameter("endNormalPrice");
        String startMarketPrice = req.getParameter("startMarketPrice");
        String endMarketPrice = req.getParameter("endMarketPrice");
        String cuisineId = req.getParameter("cuisineId");
        String enable = req.getParameter("enable");
        String startCreateTime = req.getParameter("startCreateTime");
        String endCreateTime = req.getParameter("endCreateTime");
//        3.封装到model
        ProductModel model = new ProductModel();
        model.setPage(Tool.strToPage(page));
        model.setLimit(Tool.strToLimit(limit));
        model.setName(Tool.nullToStr(name));
        model.setCreateTimeStart(Tool.nullToDate(startCreateTime));
        model.setCreateTimeEnd(Tool.nullToDate(endCreateTime));
        model.setStartMarketPrice(Tool.nullToDecimal(startMarketPrice));
        model.setEndMarketPrice(Tool.nullToDecimal(endMarketPrice));
        model.setStartNormalPrice(Tool.nullToDecimal(startNormalPrice));
        model.setEndNormalPrice(Tool.nullToDecimal(endNormalPrice));
        model.setCuisineId(Tool.strToInt(cuisineId));
        model.setEnable(Tool.strToInt(enable));
//        4.调用服务逻辑层
        ProductService productService = new ProductServiceImpl();
//        5.得到返回值
        ResultModel resultModel = productService.findAll(model);
//        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));

    }
}
